package com.example.cinema.activity;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cinema.BR;
import com.example.cinema.R;
import com.example.cinema.UserAttribute;
import com.example.cinema.adapter.CommentAdapter;
import com.example.cinema.adapter.CommentRecyAdapter;
import com.example.cinema.bindingdata.VideoData;
import com.example.cinema.databinding.ActivitySearchBinding;
import com.example.cinema.request.RequestData;

import java.util.ArrayList;
import java.util.List;

//搜索页
public class ActivitySearch extends AppCompatActivity {

    private ActivitySearchBinding ASB;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ASB = DataBindingUtil.setContentView(this, R.layout.activity_search);
        init();
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = getWindow().getDecorView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }
    }


    private CommentRecyAdapter adapter;
    private List<VideoData> listdata = new ArrayList<>();
    private UserAttribute attribute;
    private int pag = 1;
    private int lim = 15;
    private String text;
    private void init()
    {
        ASB.searchcontext.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));


        adapter = new CommentRecyAdapter<VideoData>(listdata,getApplicationContext(),R.layout.item_list_view,BR.listvideodata);
        ASB.searchcontext.setAdapter(adapter);
        attribute = (UserAttribute) getApplication();

        //换一批
        ASB.upcai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_search_up);
                animation.setDuration(1000);
                ASB.xzim.startAnimation(animation);
                Log.i("HHH","点击");
                new Thread(new TaskAnimation()).start();
            }
        });
        //搜索
        ASB.searchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pag = 1;
                listdata.clear();
                if(attribute.getToken()==null||attribute.getUID()==null)
                {
                    Intent intent = new Intent();
                    intent.setAction("sign");
                    startActivity(intent);
                    return;
                }

                ASB.animatelong.setVisibility(View.VISIBLE);
                HcAnimator();
                ASB.searchkeypush.setVisibility(View.GONE);
                text = ASB.editContext.getText().toString();
                if(text.isEmpty())
                {
                    Toast.makeText(getApplicationContext(),"请输入搜索内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                GetRequData(text);
            }
        });
        //返回
        ASB.finsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //搜索框焦点事件
        ASB.topsearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

            }
        });
        //滚动
        ASB.searchcontext.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int h = recyclerView.getMeasuredHeight();
                if(dy>=h)
                {
                    pag++;
                    GetRequData(text);
                }

            }
        });
        //选择
        adapter.setOnItemViewClick(new CommentRecyAdapter.OnItemViewClick() {
            @Override
            public void ItemViewClick(View view, int position) {

                if(attribute.isVipbool())
                {

                    Intent intent = new Intent();
                    intent.setAction("videoplayer");
                    Bundle bundle = new Bundle();
                    bundle.putString("title", listdata.get(position).videoname.get());
                    bundle.putString("vid", listdata.get(position).videosid.get());
                    Log.i("AAAA", listdata.get(position).videosid.get());
                    bundle.putString("brief", listdata.get(position).videobrief.get());
                    bundle.putString("type", listdata.get(position).videotype.get());
                    bundle.putString("urls", listdata.get(position).videourls.get());
                    Log.i("AAAA", listdata.get(position).videourls.get());
                    bundle.putString("imglist", listdata.get(position).videoimge.get());
                    bundle.putString("uimg", listdata.get(position).videouimg.get());
                    bundle.putString("love", listdata.get(position).videolove.get());
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else {
                    Dialog();
                }


            }
        });


    }
    //非VIP提醒
    private AlertDialog alertDialogw;

    private void Dialog() {
        int w = getResources().getDisplayMetrics().widthPixels;
        int h = getResources().getDisplayMetrics().heightPixels;
        View view = LinearLayout.inflate(getApplicationContext(), R.layout.dialog_item, null);

        LinearLayout.LayoutParams paramsa = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsa.width = w / 5 * 2;
        paramsa.height = h / 4 * 3;
        view.setLayoutParams(paramsa);
        int wi = getResources().getDisplayMetrics().widthPixels;
        alertDialogw = new AlertDialog.Builder(ActivitySearch.this)
                .setView(view)
                .setCancelable(false)
                .show();
        Window window = alertDialogw.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.getDecorView().setBackgroundColor(Color.argb(0, 0, 0, 0));
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = wi / 4 * 3;
        window.setAttributes(params);

        View vipview = view.findViewById(R.id.vipbtn);
        View wymf = view.findViewById(R.id.mfbtn);
        View close = view.findViewById(R.id.closebtn);
        vipview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳到VIP充值
                alertDialogw.cancel();
                Intent intent = new Intent();
                intent.setAction("cartpay");
                startActivity(intent);
            }
        });
        wymf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳到推广分享

                alertDialogw.cancel();
                Intent intent = new Intent();
                intent.setAction("spread");
                startActivity(intent);
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialogw.cancel();
            }
        });
    }
    //换一批动画
    Animation animation;
    class TaskAnimation implements Runnable {
        @Override
        public void run() {

        }
    }
    //缓冲动画
    ValueAnimator vanimator;
    private void HcAnimator()
    {
        vanimator = ValueAnimator.ofFloat(360);
        vanimator.setDuration(1000);
        vanimator.setInterpolator(new LinearInterpolator());
        vanimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float v = (float) animation.getAnimatedValue();
                ASB.huanchong.setRotation(v);
                if(v==360)
                {
                    HcAnimator();
                }
            }
        });
        vanimator.start();
    }
    //请求数据
    private void GetRequData(String text)
    {
        new Thread()
        {
            @Override
            public void run() {
                RequestData requestData = new RequestData(getApplicationContext());
                List<VideoData> list = requestData.SerchData(text,pag,lim,attribute);
                Log.i("PPP","获取到数据"+list.size());
                if(list.size()<1)
                {
                    return;
                }
                listdata.addAll(list);
                handler.sendEmptyMessage(1000);
                Log.i("PPP","更新完毕");
            }
        }.start();
    }
    //更新
    Handler handler = new Handler(Looper.getMainLooper())
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what==1000)
            {
                Log.i("VVVV","更新");
                ASB.animatelong.setVisibility(View.GONE);
                adapter.notifyDataSetChanged();
            }
        }
    };
}
